import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { Icon } from '/@/components/Icon';

export const columns: BasicColumn[] = [
  {
    title: '菜单名称',
    dataIndex: 'menuTitle',
    width: 200,
    align: 'left',
  },
  {
    title: '菜单编码',
    dataIndex: 'menuCode',
    width: 200,
    align: 'left',
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '路由',
    dataIndex: 'routerPath',
    align: 'left',
  },
  {
    title: '组件',
    dataIndex: 'componentPath',
    align: 'left',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '状态',
    dataIndex: 'stateId',
    width: 80,
    customRender: ({ record }) => {
      const stateId = record.stateId;
      const enable = ~~stateId === 1;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createdDate',
    width: 180,
  },
];

// const isDir = (type: number) => type === 0;
const isMenu = (type: number) => type === 1;
const isButton = (type: number) => type === 2;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'menuTitle',
    label: '菜单名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '目录', value: 0 },
        { label: '菜单', value: 1 },
      ],
    },
    colProps: { lg: 24, md: 24 },
    required: true,
  },
  {
    field: 'menuTitle',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'menuCode',
    label: '菜单编码',
    component: 'Input',
    required: true,
  },
  {
    field: 'parentId',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'menuTitle',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'routerPath',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'routerName',
    label: '路由名称',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'componentPath',
    label: '组件路径',
    component: 'Input',
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'redirect',
    label: '重定向路径',
    component: 'Input',
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'permissionTag',
    label: '权限标识',
    component: 'Input',
    ifShow: ({ values }) => isButton(values.type) || isMenu(values.type),
    required: true,
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'stateId',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ],
    },
    required: true,
  },
  {
    field: 'isExtId',
    label: '是否外链',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'hideMenuId',
    label: '是否隐藏菜单',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'hideTabId',
    label: '是否TAB中隐藏',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'hideBreadcrumbId',
    label: '是否隐藏面包屑',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'hideChildrenInMenuId',
    label: '是否隐藏子菜单',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
    required: true,
  },
  {
    field: 'keepAliveId',
    label: '是否缓存',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    ifShow: ({ values }) => isMenu(values.type),
    required: true,
  },
];

export const apiColumns: any[] = [
  {
    title: '序号',
    dataIndex: 'serialNumber',
    width: '5%',
  },
  {
    title: '接口名称',
    dataIndex: 'apiTitle',
    width: '15%',
    ellipsis: true,
  },
  {
    title: '接口编码',
    dataIndex: 'apiCode',
    width: '15%',
    ellipsis: true,
  },
  {
    title: '请求方式',
    dataIndex: 'apiHttpMethod',
    width: '5%',
  },
  {
    title: '接口地址',
    dataIndex: 'apiUrl',
    width: '15%',
    ellipsis: true,
  },
  {
    title: '请求参数',
    dataIndex: 'apiParam',
    width: '15%',
    ellipsis: true,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: '5%',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    width: '5%',
  },
];
